package com.product.controller.admin;

import com.product.pojo.dto.SetmealDTO;
import com.product.pojo.dto.SetmealPageQueryDTO;
import com.product.pojo.vo.SetmealVO;
import com.product.service.SetmealService;

import com.sky.result.PageResult;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags="套餐相关接口")
@RequestMapping("admin/setmeal")
public class SetMealController {
       @Autowired
       SetmealService setmealService;

       /**
        * 新增菜品
        * @param setmealDTO
        * @return
        */
       @PostMapping
       @ApiOperation("新增菜品")
       @CacheEvict(cacheNames = "setMeal",key="#setmealDTO.categoryId")
       //接受的json
       public Result save(@RequestBody SetmealDTO setmealDTO) {
              log.info("增加套餐信息：{}",setmealDTO.toString());
              setmealService.insert(setmealDTO);
              return Result.success();
       }

       /**
        * 分页查询
        * @param setmealPageQueryDTO
        * @return
        */
       @GetMapping("page")
       @ApiOperation("分页查询")
       public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
              log.info("分页查询；{}",setmealPageQueryDTO.toString());
              PageResult pageResult=setmealService.page(setmealPageQueryDTO);
              return Result.success(pageResult);
       }

       /**
        *
        * 修改销售状态
        * @param status
        * @param id
        * @return
        */
       @PostMapping("status/{status}")
       @ApiOperation("修改销售状态")
       @CacheEvict(cacheNames = "setMeal",allEntries = true)
       public Result updateStatusById(@PathVariable int status,@RequestParam String id) {
              log.info("修改后的状态:{}",status==1?"起售":"停售");
              setmealService.updateStatusById(status,id);
              return Result.success();
       }

       /**
        * 根据id获取套餐信息
        * @param id
        * @return
        */
       @GetMapping("{id}")
       @ApiOperation("根据id获取套餐信息")
       public Result<SetmealVO> selectById(@PathVariable Long id) {
              log.info("套餐id:",id);
              SetmealVO setmealVO= setmealService.selectById(id);
              log.info(setmealVO.toString());
              return Result.success(setmealVO);
       }

       /**
        * 修改套餐
        * @param setmealDTO
        * @return
        */
       @PutMapping()
       @ApiOperation("更新套餐")
       @CacheEvict(cacheNames = "setMeal",allEntries = true)
       public Result update(@RequestBody SetmealDTO  setmealDTO){
              setmealService.update(setmealDTO);
              return Result.success();
       }

       /**
        * 批量删除套餐
        * @param ids
        * @return
        */
       @DeleteMapping()
       @ApiOperation("根据ids批量删除套餐")
       @CacheEvict(cacheNames = "setMeal",allEntries = true)
       public Result delete(@RequestParam List<Long> ids) {
              log.info("批量删除的套餐id:{}",ids.toString());
              setmealService.delete(ids);
              return Result.success();
       }

       /**
        * map统计数量
        * @param map
        * @return
        */
       @GetMapping("countMap")
       @ApiOperation("根据map统计菜品数量")
       public Result<Integer> countByMap(@RequestParam Map<String,Object> map) {
              return setmealService.countByMap(map);
       }


}
